#include "tcpserver.hpp"

int main()
{
    std::string serverip = "110.40.209.26";
    uint16_t serverport = 8888;

    //创建套接字
    int fd = socket(AF_INET,SOCK_STREAM,0);
    if(fd < 0){
        lg(Fatal,"create socket failed");
        exit(1);
    }


    //发起连接
    struct sockaddr_in server;
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    socklen_t len = sizeof(server);
    int n =connect(fd,(struct sockaddr*)&server,len);
    if(n < 0){
        lg(Fatal,"client connect failed");
        exit(2);
    }

    while(true){
        std::string message;
        std::cout<< "Please Enter@";
        std::getline(std::cin,message);

        int n =send(fd,message.c_str(),message.size(),0);
        if(n < 0){
            lg(Warning,"client send failed");
            continue;
        }

        char buf[1029] = {0};
        n = recv(fd,buf,sizeof(buf)-1,0);
        if(n < 0){
             lg(Warning,"client recv failed");
             break;
        }else if(n == 0){
             break;
        }

        std::cout<<buf<<std::endl;
    }


    return 0;
}